﻿using System;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace Gui.Functies.Takenlijst
{
    public partial class Edittaak2 : UserControl
    {
        private int projectID;
        private string taaknaam;
        private int taakID;
        private string nieuwenaam;
        private static string conn = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
        MySqlConnection grabConnection = new MySqlConnection(conn);

        public Edittaak2()
        {
            
        }
        //Constructor.
        public Edittaak2(int projectnummer, int taskid, string taskname)
        {
            projectID = projectnummer;
            taaknaam = taskname;
            taakID = taskid;
            InitializeComponent();
        }

        //Roept de methode aan om de taak op te slaan.
        private void btnEdit_Click_1(object sender, EventArgs e)
        {
            nieuwenaam = textBox1.Text;

            if (nieuwenaam != "")
            {
                SaveTask();
            }
            else
            {
                MessageBox.Show("Niet alle velden zijn ingevuld!");
            }
        }
        //Slaat de taak op.
        public void SaveTask()
        {
            MySqlCommand command = new MySqlCommand("Update taken SET Taak=@nieuwenaam WHERE TaakID=@taakid", grabConnection);
            command.Parameters.Add(new MySqlParameter("nieuwenaam", nieuwenaam));
            command.Parameters.Add(new MySqlParameter("taakid", taakID));
            
            try
            {
                grabConnection.Open();
                command.ExecuteNonQuery();
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                grabConnection.Close();
                this.Controls.Clear();
                this.Controls.Add(new Takenlijst(projectID));
            }
        }   
    }
}
